/***************************************************************************
 *   Copyright (C) 2008 by Rick Olson   *
 *   rsolson@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "element.h"

namespace solve
{

	Element::Element()
	{
	}


	Element::~Element()
	{
	}

	Element::extractElement ( Element *el, quint8 *ptr )
	{
		quint8 *tmpptr;
		short length;

		if ( ptr == 0 )
		{
			throw 43;
		}

		tmpptr = ptr;

		char type = * ( astype_e * ) tmpptr;
		tmpptr++;

		switch ( ( astype_e ) type )
		{
			case Element::NUMBER:
				el->makeNumber ( tmpptr );
				tmpptr += 8;
				break;
			case Element::BOOLEAN:
				el->makeBoolean ( tmpptr );
				tmpptr += 2;
				break;

			case Element::STRING:
				length = ntohs ( ( * ( short * ) tmpptr ) & 0xffff );
				tmpptr += 2;
				if ( length > 0 )
				{
					el->makeString ( tmpptr, length );
					tmpptr+=length;
				}
				break;
      case Element::OBJECT:
        qDebug() << "Encountered unsupported OBJECT type";
/**
	  do {
	      tmpptr = extractVariable(el, tmpptr);
	  } while (el->getType() != Element::OBJECT_END);
*/
	  break;
      case Element::MOVIECLIP:
      case Element::NULL_VALUE: 
      case Element::UNDEFINED:
      case Element::REFERENCE:
      case Element::ECMA_ARRAY:
      case Element::OBJECT_END:
      case Element::STRICT_ARRAY:
      case Element::DATE:
      case Element::LONG_STRING:
      case Element::UNSUPPORTED:
      case Element::RECORD_SET:
      case Element::XML_OBJECT:
      case Element::TYPED_OBJECT:
      default:

	  return 0;
		}

		return tmpptr;
	}

	Element & 
	Element::makeString ( quint8 *data, int size )
	{
		_type = Element::STRING;
		_length = size;
		qDebug() << "elsize: " << _length;
		_data = new quint8[size+1];
		memset ( _data, 0, size+1 );
		memcpy ( _data, data, size );
		return *this;
	}

	Element &
	Element::makeNumber ( quint8 *data )
	{
		_type = Element::NUMBER;
		_length = 8;
		_data = new quint8[8];
		memcpy ( _data, data, 8 );
		return *this;
	}

	Element &
	Element::makeBoolean ( bool &data )
	{
		_type = Element::BOOLEAN;
		_length = 1;
		_data = new quint8[2];
		memset ( _data, 0, 2 );
		_data[1]= data;
		return *this;
	}

	Element &
	Element::makeBoolean ( quint8 *data )
	{
		_type = Element::BOOLEAN;
		_length = 1;
		_data = new quint8[2];
		memset ( _data, 0, 2 );
		memcpy ( _data, data+1, 1 );
		return *this;
	}

}
